package com.kejiguan.ui.presenter;

import android.widget.Toast;

import com.android.volley.error.VolleyError;
import com.android.volley.response.Response;
import com.kejiguan.ui.model.NeItem;
import com.kejiguan.ui.request.GsonRequest;
import com.kejiguan.ui.request.RequestUtil;
import com.kejiguan.ui.response.NeItemManagerResponse;
import com.kejiguan.ui.response.QueryAllNeItemResponse;
import com.kejiguan.ui.util.CacheDataUtil;
import com.kejiguan.ui.util.CommonDate;
import com.kejiguan.ui.util.DateFormatUtil;

import org.greenrobot.eventbus.EventBus;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by YYY on 16/7/18.
 */
public class NeItemManagerPresenter extends BaseFragmentPresenter{


    public void queryNeItems()
    {

        long currentTime=System.currentTimeMillis();
        String currentDate=String.valueOf(currentTime);
        try {
            currentDate= DateFormatUtil.longToString(currentTime,"yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Map<String,String> params=new HashMap<>();
        params.put("requestCode","705");
        params.put("messageId",currentDate+"S"+currentTime);
        params.put("systemCode", CommonDate.SystemCode);
        params.put("userName", CacheDataUtil.getInstance().getmUser().getUserName());
        params.put("operType","queryList");


        final GsonRequest<QueryAllNeItemResponse> request = new GsonRequest("QueryItems", QueryAllNeItemResponse.class, new Response.Listener<QueryAllNeItemResponse>() {
            @Override
            public void onResponse(QueryAllNeItemResponse response) {

                response.initEmptyNeItems();
                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(mContext,"新增网元失败！",Toast.LENGTH_SHORT).show();

            }
        });

        RequestUtil.sendRequest(mContext, params, request);

    }


    public void addNeItem(final NeItem neItem)
    {

        long currentTime=System.currentTimeMillis();
        String currentDate=String.valueOf(currentTime);
        try {
            currentDate= DateFormatUtil.longToString(currentTime,"yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Map<String,String> params=new HashMap<>();
        params.put("requestCode","705");
        params.put("messageId",currentDate+"S"+currentTime);
        params.put("systemCode", CommonDate.SystemCode);
        params.put("userName", CacheDataUtil.getInstance().getmUser().getUserName());
        params.put("operType","add");

        params.put("neId",neItem.getId());
        params.put("neName",neItem.getNeName());
        params.put("neIp",neItem.getNeIp());
        params.put("nePort",neItem.getNePort());
        params.put("neType",neItem.getNeType());
        params.put("aliveTime","5");
        params.put("mac",neItem.getMac());
        params.put("broadcastIp","255.255.255.255");
        params.put("neNote",neItem.getNote());


        final GsonRequest<NeItemManagerResponse> request = new GsonRequest("AddNeItem", NeItemManagerResponse.class, new Response.Listener<NeItemManagerResponse>() {
            @Override
            public void onResponse(NeItemManagerResponse response) {

                response.setNeItem(neItem);
                response.setOperType("add");
                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(mContext,"新增网元失败！",Toast.LENGTH_SHORT).show();

            }
        });

        RequestUtil.sendRequest(mContext, params, request);

    }


    public void modifyNeItem(final NeItem neItem)
    {

        long currentTime=System.currentTimeMillis();
        String currentDate=String.valueOf(currentTime);
        try {
            currentDate= DateFormatUtil.longToString(currentTime,"yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Map<String,String> params=new HashMap<>();
        params.put("requestCode","705");
        params.put("messageId",currentDate+"S"+currentTime);
        params.put("systemCode",CommonDate.SystemCode);
        params.put("userName", CacheDataUtil.getInstance().getmUser().getUserName());
        params.put("operType","mod");

        params.put("neId",neItem.getId());
        params.put("wpaId",neItem.getWpaId());
        params.put("neName",neItem.getNeName());
        params.put("neIp",neItem.getNeIp());
        params.put("nePort",neItem.getNePort());
        params.put("neType",neItem.getNeType());
        params.put("aliveTime",String.valueOf(neItem.getAliveTime()));
        params.put("mac",neItem.getMac());
        params.put("broadcastIp",neItem.getBroadcastIp());
        params.put("neNote",neItem.getNote());


        final GsonRequest<NeItemManagerResponse> request = new GsonRequest("ModNeItem", NeItemManagerResponse.class, new Response.Listener<NeItemManagerResponse>() {
            @Override
            public void onResponse(NeItemManagerResponse response) {

                response.setNeItem(neItem);
                response.setOperType("mod");
                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(mContext,"修改网元失败！",Toast.LENGTH_SHORT).show();
            }
        });

        RequestUtil.sendRequest(mContext, params, request);

    }


    public void delNeItem(final NeItem neItem)
    {

        long currentTime=System.currentTimeMillis();
        String currentDate=String.valueOf(currentTime);
        try {
            currentDate= DateFormatUtil.longToString(currentTime,"yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Map<String,String> params=new HashMap<>();
        params.put("requestCode","705");
        params.put("messageId",currentDate+"S"+currentTime);
        params.put("systemCode", CommonDate.SystemCode);
        params.put("userName", CacheDataUtil.getInstance().getmUser().getUserName());
        params.put("operType","delete");
        params.put("neId",neItem.getId());

        params.put("aliveTime","5");

        final GsonRequest<NeItemManagerResponse> request = new GsonRequest("DelNeItem", NeItemManagerResponse.class, new Response.Listener<NeItemManagerResponse>() {
            @Override
            public void onResponse(NeItemManagerResponse response) {
                response.setOperType("delete");
                response.setNeItem(neItem);
                EventBus.getDefault().post(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(mContext,"删除网元失败！",Toast.LENGTH_SHORT).show();
            }
        });

        RequestUtil.sendRequest(mContext, params, request);
    }
}
